﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WorkNodeFramework
{
    public class Update:Query<Update>, IExecuteCommand
    {
        Dictionary<Column, object> setList = new Dictionary<Column, object>();
        ITable updatedTable;

        public Update(ITable table) {
            this.updatedTable = table;
        }

        public Update Set(Column column, object value) {
            setList[column] = value;
            return this;
        }

        internal Dictionary<Column, object> SetList {
            get {
                return setList;
            }
        }

        internal ITable UpdatedTable {
            get {
                return updatedTable;
            }
        }

        public string BuildUpdateQueryStatement(ParameterIndexCollection parameters)
        {
            return updatedTable.Provider.BuildUpdateQueryStatement(this, parameters);
        }

        public int Execute()
        {
            ParameterIndexCollection pars = new ParameterIndexCollection();
            string cmdText = Provider.BuildUpdateQueryStatement(this, pars);
            return updatedTable.Provider.ExecuteNonQuery(cmdText, pars.ToDbParameters(Provider));
        }

        #region IExecuteCommand Members

        public ExecuteCommand BuildExecuteCommand()
        {
            ParameterIndexCollection pars = new ParameterIndexCollection();
            string cmdText = Provider.BuildUpdateQueryStatement(this, pars);
            return new ExecuteCommand(cmdText, pars.ToDbParameters(updatedTable.Provider));
        }

        #endregion
    }
}
